package com.hundsun.yr.universal.p2p.utils;

import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.hundsun.yr.universal.library.data.cipher.HSEncrypt3Des;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.listener.BaseMultiLoadedListener;
import com.umeng.socialize.utils.Log;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: VolleyRequest.java 
 * @Description: Volley请求
 * @date 2015-10-19 下午4:09:02 
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com 
 * @replace author:
 * @replace date:
 * @version V1.0   
 */
public class VolleyRequest {
	/**是否需要解密**/
	private boolean if_encrypt=true;

	/**
	 * @param encrypt 是否需要加密
	 */
	public VolleyRequest(boolean encrypt){
		this.if_encrypt=encrypt;
	}
	
	/**
	 * String  volley-post请求
	 * @param task_name
	 * @param url
	 * @param event_tag
	 * @param params
	 * @param baseMultiLoadedListener
	 */
	public void StringRequestPost(final String task_name, final String url, final int event_tag,
								  final HashMap<String, String>  params, final BaseMultiLoadedListener<String> baseMultiLoadedListener){
		StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				if(baseMultiLoadedListener==null)return;
				// response
				if(if_encrypt && !TextUtils.isEmpty(response)){
					//通过加密的params去请求的数据
					try {
						//将获得的参数进行解密
						response=HSEncrypt3Des.decrypt3DesEncode(response,Config.ENCRYPT_KEY_1,Config.ENCRYPT_KEY_2,Config.ENCRYPT_KEY_3);
					} catch (Exception e) {
						response="";
					}
				}
				try {
					baseMultiLoadedListener.onSuccess(task_name, event_tag,response);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, new Response.ErrorListener() {
			
			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub
				if(baseMultiLoadedListener==null)return;
				baseMultiLoadedListener.onException(task_name,VolleyErrorMessage.errorMessage(error));
			}
		}){     
		    @Override
		    protected HashMap<String, String> getParams() 
		    {  
		    	return params;  
		    }
		    
		};
		request.setShouldCache(true);
		request.setTag(task_name);
		VolleyHelper.getInstance().getRequestQueue().add(request);
	}
	/**
	 * String  volley-post请求
	 * @param task_name
	 * @param url
	 * @param event_tag
	 * @param params
	 * @param baseMultiLoadedListener
	 */
	public void StringRequestPost(final String task_name,String url,final int event_tag,
			final byte[] params,final Map<String, String> headers,final BaseMultiLoadedListener<String> baseMultiLoadedListener){
		StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				if(baseMultiLoadedListener==null)return;
				try {
					baseMultiLoadedListener.onSuccess(task_name, event_tag,response);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub
				if(baseMultiLoadedListener==null)return;
				baseMultiLoadedListener.onException(task_name,VolleyErrorMessage.errorMessage(error));
			}
		}){     
			@Override  
			public byte[] getBody() throws AuthFailureError  
			{  
				return params == null ? super.getBody() : params;  
			}  
			
			@Override  
			public Map<String, String> getHeaders() throws AuthFailureError{  
				
				return headers;  
			}  
			
		};
		
		request.setShouldCache(true);
		request.setTag(task_name);
		
		VolleyHelper.getInstance().getRequestQueue().add(request);
		
	}

	/**
	 * String  volley-post请求
	 * @param task_name
	 * @param url
	 * @param event_tag
	 * @param params
	 * @param baseMultiLoadedListener
	 */
	public void StringRequestPost(final String task_name,String url,final int event_tag,
			final HashMap<String, String> params,final Map<String, String> headers,final BaseMultiLoadedListener<String> baseMultiLoadedListener){
		StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				if(baseMultiLoadedListener==null)return;
				try {
					baseMultiLoadedListener.onSuccess(task_name, event_tag,response);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, new Response.ErrorListener() {
			
			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub
				if(baseMultiLoadedListener==null)return;
				baseMultiLoadedListener.onException(task_name,VolleyErrorMessage.errorMessage(error));
			}
		}){     
			@Override
			protected HashMap<String, String> getParams() 
			{  
				return params;  
			}
			@Override  
			public Map<String, String> getHeaders() throws AuthFailureError{
				return headers;  
			}
		};
		
		request.setShouldCache(true);
		request.setTag(task_name);
		
		VolleyHelper.getInstance().getRequestQueue().add(request);
		
	}
	
	/**
	 * String  volley-get请求
	 * @param task_name
	 * @param url
	 * @param event_tag
	 * @param baseMultiLoadedListener
	 */
	public void StringRequestGet(final String task_name,String url,final int event_tag,
			final Map<String, String> headers,final BaseMultiLoadedListener<String> baseMultiLoadedListener){
		StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				if(baseMultiLoadedListener==null)return;
				try {
					baseMultiLoadedListener.onSuccess(task_name, event_tag,response);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				Log.e("error: " + error);
				// TODO Auto-generated method stub
				if(baseMultiLoadedListener==null)return;
				baseMultiLoadedListener.onException(task_name,VolleyErrorMessage.errorMessage(error));
			}
		}
		){
			@Override  
			public Map<String, String> getHeaders() throws AuthFailureError{  
				
				return headers;  
			}
		};
		
		request.setShouldCache(true);
		request.setTag(task_name);
		
		
		VolleyHelper.getInstance().getRequestQueue().add(request);
		
	}

	/**
	 * String  volley-get请求
	 * @param task_name
	 * @param url
	 * @param event_tag
	 * @param baseMultiLoadedListener
	 */
	public void StringRequestGet(final String task_name,String url,final int event_tag,final HashMap<String,String> params,
								 final Map<String, String> headers,final BaseMultiLoadedListener<String> baseMultiLoadedListener){
		StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				if(baseMultiLoadedListener==null)return;
				try {
					baseMultiLoadedListener.onSuccess(task_name, event_tag,response);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub
				if(baseMultiLoadedListener==null)return;
				baseMultiLoadedListener.onException(task_name,VolleyErrorMessage.errorMessage(error));
			}
		}
		){
			@Override
			public Map<String, String> getHeaders() throws AuthFailureError{

				return headers;
			}
			@Override
			protected HashMap<String, String> getParams()
			{
				return params;
			}
		};

		request.setShouldCache(true);
		request.setTag(task_name);
		VolleyHelper.getInstance().getRequestQueue().add(request);
	}


	/**
	 * json请求header+json
	 * @param task_name
	 * @param url
	 * @param event_tag
	 * @param params
	 * @param headers
	 * @param baseMultiLoadedListener
     */
	public void StringRequestPost(final String task_name, String url, final int event_tag,
								  final JSONObject params, final Map<String, String> headers, final BaseMultiLoadedListener<String> baseMultiLoadedListener){
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,params, new Response.Listener<JSONObject>() {
			@Override
			public void onResponse(JSONObject jsonObject) {
				if(baseMultiLoadedListener==null)return;
				try {
					baseMultiLoadedListener.onSuccess(task_name, event_tag,jsonObject.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError volleyError) {
				if(baseMultiLoadedListener==null)return;
				baseMultiLoadedListener.onException(task_name,VolleyErrorMessage.errorMessage(volleyError));
			}
		}) {
			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				return headers;
			}
		};

		request.setShouldCache(true);
		request.setTag(task_name);
		VolleyHelper.getInstance().getRequestQueue().add(request);
	}
}
